package com.gz.mylife.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gz.mylife.R;
import com.gz.mylife.app.ProductDetail;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.zl.smartmall.library.po.ShoppingCartInfo;

public class ShoppingCartAdapterBAC extends BaseAdapter{
	private List<ShoppingCartInfo> mDataList;
	private Context mContext;
	private boolean isEditable;
	private UpdataUI mUpdataUI;
	
	private DisplayImageOptions options;
	public ShoppingCartAdapterBAC(Context context,List<ShoppingCartInfo> mDataList,UpdataUI updataUI){
		this.mContext = context;
		this.mDataList = mDataList;
		this.options = buildOptions();
		this.mUpdataUI = updataUI;
	}
	
	private DisplayImageOptions buildOptions() {
		return new DisplayImageOptions.Builder()
        .resetViewBeforeLoading()
        .bitmapConfig(Bitmap.Config.ARGB_8888)
        .cacheInMemory()
        .cacheOnDisc()
        .showStubImage(R.drawable.drawable_recent_white)
        .showImageOnFail(R.drawable.drawable_recent_white)
        .imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
        .displayer(new RoundedBitmapDisplayer(5))
        .build();
	}
	

	public void setEditable(boolean isEditable) {
		this.isEditable = isEditable;
		notifyDataSetChanged();
	}



	@Override
	public int getCount() {
		return mDataList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
			ViewHold holder;
			if(convertView == null){
				holder = new ViewHold();
				convertView = View.inflate(mContext, R.layout.shopping_cart_item, null);
				holder.pic = (ImageView) convertView.findViewById(R.id.iv_product_pic);
				holder.title = (TextView) convertView.findViewById(R.id.tv_product_title);
				holder.name = (TextView) convertView.findViewById(R.id.tv_product_name);
				holder.price = (TextView) convertView.findViewById(R.id.tv_product_price);
				holder.selectBtn = (ImageView) convertView.findViewById(R.id.btn_product_selected);
				holder.minus = (Button) convertView.findViewById(R.id.btn_minus);
				holder.num = (TextView) convertView.findViewById(R.id.tv_buy_num);
				holder.add = (Button) convertView.findViewById(R.id.btn_add);
				holder.tvBuyNum = (TextView) convertView.findViewById(R.id.tv_buy_num2);
				holder.tvTitleCount = (TextView) convertView.findViewById(R.id.tv_title_count);
				holder.bgAddandminus = (RelativeLayout) convertView.findViewById(R.id.rl_bottom2);
				convertView.setTag(holder);
			}else{
				holder = (ViewHold) convertView.getTag();
			}
			final ShoppingCartInfo info = mDataList.get(position);
			
			holder.title.setText(info.getProductTitle());
			holder.name.setText(info.getShopName());
			holder.price.setText(info.getSalePrice()+"");
			holder.num.setText(info.getBuyNum()+info.getAddNum()+"");
			holder.tvBuyNum.setText(info.getBuyNum()+info.getAddNum()+"");
			if(!isEditable){
				holder.tvBuyNum.setVisibility(View.VISIBLE);
				holder.tvTitleCount.setVisibility(View.GONE);
				holder.bgAddandminus.setVisibility(View.GONE);
			}else{
				holder.tvBuyNum.setVisibility(View.GONE);
				holder.tvTitleCount.setVisibility(View.VISIBLE);
				holder.bgAddandminus.setVisibility(View.VISIBLE);
			}
			holder.title.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					if(!isEditable){
						Intent intent = new Intent(mContext, ProductDetail.class);
						intent.putExtra("productId", info.getProductId());
						intent.putExtra("shopId", info.getShopId());
						intent.putExtra("mBrandId", 0);
						mContext.startActivity(intent);
					}
				}
			});
			holder.pic.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					if(!isEditable){
						Intent intent = new Intent(mContext, ProductDetail.class);
						intent.putExtra("productId", info.getProductId());
						intent.putExtra("shopId", info.getShopId());
						intent.putExtra("mBrandId", 0);
						mContext.startActivity(intent);
					}
				}
			});
			if(info.isSeleted()){
				holder.selectBtn.setBackgroundResource(R.drawable.ic_shopping_selected);
			}else{
				holder.selectBtn.setBackgroundResource(R.drawable.ic_shopping_unselected);
			}
			
			holder.selectBtn.setTag(info);
			holder.selectBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					ShoppingCartInfo info2 = (ShoppingCartInfo) v.getTag();
					if(info2.isSeleted()){
						info2.setSeleted(false);
						if(info2.getProductId() == info.getProductId()){
							v.setBackgroundResource(R.drawable.ic_shopping_unselected);
						}
					}else{
						info2.setSeleted(true);
						if(info2.getProductId() == info.getProductId()){
							v.setBackgroundResource(R.drawable.ic_shopping_selected);
						}
					}
//					countIsAllSeleted();
					mUpdataUI.update();
				}

				
			});
			holder.num.setTag(info);
			holder.minus.setTag(holder.num);
			holder.minus.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					    TextView productNum = (TextView)v.getTag();
					    ShoppingCartInfo mInfoSeleted = (ShoppingCartInfo) productNum.getTag();
//						mReqCode = REQ_MINUS;
//						mLoadingData.setVisibility(View.VISIBLE);
//						loadDataFromServer();
					    int addNum = mInfoSeleted.getAddNum();
					    addNum--;
					    if(mInfoSeleted.getBuyNum()+addNum>0){
					    	 mInfoSeleted.setAddNum(addNum);
							 productNum.setText(mInfoSeleted.getBuyNum()+addNum+"");
					    }else{
					    	return;
					    }
					    if(mInfoSeleted.isSeleted()){
					    	mUpdataUI.update();
					    }
				}
			});
			holder.num.setTag(info);
			holder.add.setTag(holder.num);
			holder.add.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
						TextView productNum = (TextView)v.getTag();
						ShoppingCartInfo mInfoSeleted = (ShoppingCartInfo) productNum.getTag();
//						mReqCode = REQ_ADD;
//						mLoadingData.setVisibility(View.VISIBLE);
//						loadDataFromServer();
					    int addNum = mInfoSeleted.getAddNum();
					    addNum++;
					    if(mInfoSeleted.getBuyNum()+addNum>0&&mInfoSeleted.getBuyNum()+addNum<=mInfoSeleted.getReserveNum()){
					    	 mInfoSeleted.setAddNum(addNum);
							 productNum.setText(mInfoSeleted.getBuyNum()+addNum+"");
					    }else{
					    	addNum--;
					    }
					    if(mInfoSeleted.isSeleted()){
					    	mUpdataUI.update();
					    }
					   
					}
			});
			 holder.pic.setTag(info.getPic());
			ImageLoader.getInstance().displayImage(info.getPic(), holder.pic, options);
		return convertView;
	}
	
	class ViewHold{
		ImageView pic;
		TextView title;
		TextView name;
		TextView price;
		ImageView selectBtn;
		Button minus;//2
		TextView num;//2
		Button add;//2
		TextView tvBuyNum; //1
		TextView tvTitleCount;//2
		RelativeLayout bgAddandminus;
	}
	
	public static interface UpdataUI{ 
		public void update();
	}
}
